<script setup>
import { ref,onMounted } from 'vue'
import { queryUserAll,deleteUser } from '@/api/user'
import { ElMessage } from 'element-plus'
//添加子组件
import OperateUser from './operateUser.vue'


//分页数据显示
const getUserAll =async ()=>{
  const result = await queryUserAll(page.value,pageSize.value,userDate.value.name,userDate.value.userCode,userDate.value.role)
  userInfo.value = result.data.rows
  total.value = result.data.total
}

onMounted(()=>{
  getUserAll()
})

//定义表单绑定的数据
const userDate = ref({name:'',userCode:'',role:''})
//定义清空的点击事件
const clear=()=>{
  userDate.value={name:'',userCode:'',role:''}
  getUserAll()
}

//定义表格显示数据
const userInfo = ref([])
//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)

//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  getUserAll()
}
const handleCurrentChange = (val) => {
  getUserAll()
}
//添加用户信息
//关联子组件
const childRef = ref(null)
const saveUser = () => { 
  childRef.value.saveUserData()
}
//回显用户信息
const edit = (id) =>{
  childRef.value.editUser(id)
}
//删除用户
const deleteById = async (id) =>{ 
  const result = await deleteUser(id)
  if(result.code){
    ElMessage.success('删除成功！')
    //重新加载数据
    getUserAll()
  }else{
    ElMessage.error('删除失败！请重新操作')
  }
}


</script>

<template>
<div>
    <el-form :inline="true" :model="userDate" class="demo-form-inline">
    <el-form-item>
      <el-button type="primary" @click="saveUser" plain><el-icon><Plus /></el-icon></el-button>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="userDate.name" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="员工编号">
      <el-input v-model="userDate.userCode" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="角色">
      <el-input v-model="userDate.role" placeholder="请输入角色" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getUserAll" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<div>
  <el-table :data="userInfo" border style="width: 100%" >
    <el-table-column type="index" label="序号" align="center" />
    <el-table-column prop="userCode" label="员工编号" align="center" />
    <el-table-column prop="name" label="姓名" align="center" />
    <el-table-column prop="role" label="角色" align="center" />
    <el-table-column label="性别" align="center">
      <template #default="scope">
        {{ scope.row.gender == 1 ? '男' : '女' }}
      </template>
    </el-table-column>
    <el-table-column prop="phone" label="联系方式" align="center"/>
    <el-table-column label="是否有效" align="center">
      <template #default="scope">
        {{ scope.row.status == 1 ? '有效' : '无效' }}
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" size="small" @click="edit(scope.row.id)" plain><el-icon><EditPen /></el-icon></el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row.id)" plain><el-icon><Delete /></el-icon></el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<div>
     <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</div>

  <!-- 引用组件 -->
  <OperateUser ref='childRef' @search='getUserAll'/>
</template>

<style scoped>

</style>